package cn.shop.merchant.config;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.SaSessionCustomUtil;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.shop.merchant.service.MerchantRoleService;
import cn.shop.merchant.service.MerchantUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : 李一涵
 * @date : Created in 2021-11-11 13:27
 * @description :
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private MerchantUserService userService;
    @Resource
    private MerchantRoleService roleService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> permissions = new ArrayList<>();

        for (String roleCode : getRoleList(loginId, loginType)) {
            SaSession session = SaSessionCustomUtil.getSessionById("role-" + roleCode);
            List<String> list = session.get("Permission_List", () -> roleService.permissionCodeByRoleCode(roleCode));
            permissions.addAll(list);
        }
        return permissions;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        SaSession session = StpUtil.getSessionByLoginId(loginId);
        return session.get("Role_List", () -> userService.roleCodeByUserId(Convert.toLong(loginId)));
    }
}
